package com.ruoyi.web.controller.pay;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.pay.service.AliPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/api/ali-pay")
@Api(tags = "支付宝支付")
public class AliPayController {


    @Resource
    private AliPayService aliPayService;

    @PostMapping("/trade/page/pay/{productId}")
    @ApiOperation("统一收单下单并支付接口")
    public R tradePagePay(@PathVariable Long productId) {

        log.info("统一收单下单并支付接口");

        /**
         * 支付宝开发平台接收request请求
         * 返回一个html form给用户从而展示支付页面 包含自动提交
         */
        String formStr = aliPayService.tradeCreate(productId);
        return R.ok(formStr);
    }

    @ApiOperation("用户取消订单")
    @PostMapping("/trade/cancel/{orderNo}")
    public R cancel(@PathVariable String orderNo) {
        log.info("取消订单===> {}",orderNo);
        aliPayService.cancelOrder(orderNo);

        return R.ok();
    }

    @ApiOperation("查询订单接口")
    @PostMapping("/trade/query/{orderNo}")
    public R query(@PathVariable String orderNo) {
        log.info("查询订单===> {}",orderNo);

        String response = aliPayService.queryOrder(orderNo);
        return R.ok(response);
    }

    @ApiOperation("申请退款接口")
    @PostMapping("/trade/refund/{orderNo}/{reason}")
    public R refund(@PathVariable String orderNo,@PathVariable String reason) {
        log.info("申请退款订单===> {}",orderNo);

        aliPayService.refund(orderNo,reason);
        return R.ok();
    }


    @ApiOperation("获取账单url")
    @GetMapping("/bill/downloadurl/query/{billDate}/{type}")
    public R getBillUrl(@PathVariable String billDate,@PathVariable String type) {
        log.info("获取账单url===> {},{}",billDate,type);
        String downloadUrl = aliPayService.queryBillUrl(billDate,type);
        return R.ok(downloadUrl);
    }
}
